<template>
	<view class="page" :style="loading ? 'opacity: 0;' : 'opacity: 1;'">
		<!--  v-model="dataList" @query="queryList" -->
		<pageTxt>
			<z-paging ref="paging" :paging-style="{ 'background-color': '#f7f7f7', height: '100%', width: '100%' }">
				<!-- 头部 -->
				<template slot="top">
					<view class="navbar-box">
						<u-navbar :title="`“${dataObj.node_name}”验收`" :autoBack="true" :fixed="false"></u-navbar>
					</view>
				</template>
				<view class="add-content ios">
					<u--form labelPosition="top" :model="jdysRuleForm" ref="uForm" labelWidth="auto">
						<view class="form-box">
							<template v-for="(item, index) in jdysFormList">
								<u-form-item labelPosition="top" labelWidth="100%"
									:key="index"
									v-if="item.content_type == 1"
									:required="item.is_required == 1"
									:label="`${item.name}${item.supervision_acceptance_status == 2 ? '（已通过）' : ''}`"
									:borderBottom="index != jdysFormList.length - 1"
								>
									<view v-if="!jdysRuleForm[item.prop].value && item.supervision_acceptance_status == 2">未提交任何内容</view>
									<u--textarea
										:disabled="item.supervision_acceptance_status == 2"
										disableDefaultPadding
										v-model="jdysRuleForm[item.prop].value"
										border="none"
										:placeholder="`请输入${item.name}`"
										:placeholderStyle="{ fontSize: '13.5px', color: '#BFC4CC' }"
									></u--textarea>
									<text v-if="item.reason_rejection" class="reason-rejection">审批意见：{{ item.reason_rejection }}</text>
								</u-form-item>
								<u-form-item labelPosition="top" labelWidth="100%"
									:key="index"
									v-else
									:required="item.is_required == 1"
									:label="`${item.name}${item.supervision_acceptance_status == 2 ? '（已通过）' : ''}`"
									:borderBottom="index != jdysFormList.length - 1"
								>
									<view v-if="!jdysRuleForm[item.prop].value2.length && item.supervision_acceptance_status == 2">未提交任何内容</view>
									<file-upload
										:filesList="jdysRuleForm[item.prop].value2"
										:disabled="item.supervision_acceptance_status == 2"
										:type="3"
										@afterRead="afterRead(item.prop, $event)"
										@delete="deletePic(item.prop, $event)"
									></file-upload>
									<text v-if="item.reason_rejection" class="reason-rejection">审批意见：{{ item.reason_rejection }}</text>
								</u-form-item>
							</template>
						</view>
					</u--form>
				</view>
				<view class="footer ios">
					<u-button
						text="提交"
						@click="submit"
						customStyle="width: 702rpx;height: 88rpx;background: #3B75FF;border-radius: 16rpx;font-weight: 500;font-size: 28rpx;color: #FFFFFF;margin:20rpx 24rpx;"
					></u-button>
				</view>
			</z-paging>
		</pageTxt>

		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
export default {
	data() {
		return {
			loading: false,
			jdysRuleForm: {},
			dataObj: '',
			projectNodesItem: '',
			jdysFormList: []
		};
	},
	onLoad(option) {
		this.qualityAcceptanceDetails(option.id);
	},
	methods: {
		// 详情
		qualityAcceptanceDetails(node_id) {
			return new Promise((resolve, reject) => {
				this.$Apipc
					.qualityAcceptanceDetails({
						node_id
					})
					.then((res) => {
						console.log(res, '节点详情');
						if (res.code == 200) {
							// 判断当前需要验收的节点
							this.projectNodesItem = res.data.criteria_list;
							this.dataObj = res.data;
							this.$nextTick(() => {
								this.jdysShouwFunc();
							});
						}
					});
			});
		},
		// 节点验收（表单处理）
		jdysShouwFunc() {
			let projectNodesItem = this.projectNodesItem;
			console.log('节点', projectNodesItem);
			let jdysRuleForm = {};
			let jdysFormList = [];

			projectNodesItem.forEach((item) => {
				let formName = '';
				let obj = {
					name: item.node_name,
					is_required: item.is_required,
					content_type: item.content_type,
					reason_rejection: item.reason_rejection
				};
				if (item.content_type == 1) {
					// 文本
					formName = `content_${item.id}`;
					jdysRuleForm[formName] = {}; //验收初始对象数据
					jdysRuleForm[formName].is_required = item.is_required;
					jdysRuleForm[formName].supervision_acceptance_status = item.supervision_acceptance_status;
					jdysRuleForm[formName].value = item.supervision_acceptance_status == 2 ? item.content : '';
				} else {
					// 图片视频
					formName = `file_${item.id}`;
					jdysRuleForm[formName] = {}; //验收初始对象数据
					jdysRuleForm[formName].is_required = item.is_required;
					jdysRuleForm[formName].supervision_acceptance_status = item.supervision_acceptance_status;
					jdysRuleForm[formName].value = [];
					jdysRuleForm[formName].value2 = item.supervision_acceptance_status == 2 ? item.node_files : [];
				}
				obj.prop = formName;
				// 通过无需再次提交supervision_acceptance_status
				obj.supervision_acceptance_status = item.supervision_acceptance_status;
				jdysFormList.push(obj);
			});
			this.jdysFormList = jdysFormList; //formitem
			this.jdysRuleForm = jdysRuleForm; //填写数据
			console.log(jdysRuleForm);
			console.log(jdysFormList);
		},
		afterRead(type, files) {
			console.log(type, files);
			files.map((item) => {
				let obj = {
					url: item.url
				};
				this.jdysRuleForm[type].value.push(obj);
			});
		},
		deletePic(type, e) {
			console.log(e, 'deletePic');
			this.jdysRuleForm[type].value.splice(e.index, 1);
		},
		async submit() {
			let jdysRuleForm = this.jdysRuleForm;
			let projectNodesItem = this.projectNodesItem;
			let jdysFormList = this.jdysFormList;
			console.log(jdysRuleForm);
			console.log(jdysFormList);
			for (let i in jdysRuleForm) {
				if (typeof jdysRuleForm[i].value == 'string') {
					if (!jdysRuleForm[i].value && jdysRuleForm[i].supervision_acceptance_status != 2 && jdysRuleForm[i].is_required == 1) {
						uni.showToast({
							title: '请输入必填项',
							icon: 'error'
						});
						return;
					}
				} else {
					if (!jdysRuleForm[i].value.length && jdysRuleForm[i].supervision_acceptance_status != 2 && jdysRuleForm[i].is_required == 1) {
						uni.showToast({
							title: '请选择必填文件',
							icon: 'error'
						});
						return;
					}
				}
			}
			let obj = {
				project_id: this.dataObj.project_id,
				node_id: this.dataObj.id
			};
			obj.remark = ''; //描述默认为空
			projectNodesItem.forEach((item) => {
				// 已通过不再次提交(文本)
				if (item.supervision_acceptance_status != 2) {
					let key = `name_${item.id}`;
					obj[key] = item.node_name;
				}
			});
			for (let i in jdysRuleForm) {
				// 已通过不再次提交
				if (jdysRuleForm[i].supervision_acceptance_status != 2) {
					if (typeof jdysRuleForm[i].value == 'string') {
						// 文本
						obj[i] = jdysRuleForm[i].value;
					} else {
						// 文件
						obj[i] = [];
						jdysRuleForm[i].value.forEach((item) => {
							// 是否存在文件
							obj[i].push(item);
						});
					}
				}
			}
			// 文件处理
			for (let i in obj) {
				if (typeof obj[i] == 'object') {
					let file = '';
					if (obj[i].length) {
						uni.showLoading({
							title: '文件上传中',
							mask: true
						});
						file = await this.$upload(obj[i]);
						uni.hideLoading();
						obj[i] = file.arrResult;
					}
				}
			}
			this.nodeAcceptance(obj);
		},
		// 节点验收
		nodeAcceptance(data) {
			uni.showLoading({
				title: '信息提交中',
				mask: true
			});
			this.$Apipc.nodeAcceptance(data).then((res) => {
				console.log(res, '节点验收');
				uni.hideLoading();
				if (res.code == 200) {
					setTimeout(() => {
						var currentPages = getCurrentPages()[getCurrentPages().length - 2]
						if (currentPages) {
							uni.navigateBack({
								delta: 1
							});
						} else {
							uni.reLaunch({
								url: '/pages/index/index'
							});
						}
					}, 500);
				}
				this.$refs.uToast.show({
					message: res.message
				});
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.reason-rejection {
	font-size: 24rpx;
	margin: 10rpx 0;
	color: #f56c6c;
}
::v-deep .u-textarea__field {
	padding: 10rpx 20rpx;
	box-sizing: border-box;
}
::v-deep .u-form-item__body__left__content__label {
	margin-bottom: 10rpx !important;
}
</style>
